package org.atlantis.grosbeak.logger.adapter;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * JDK Logger的日志实现。
 * 
 * @author <a href="mailto:seenoevil.cn@gmail.com">SeeNoEvil</a>
 * 
 */
class JdkLogger extends BaseLogger {

	public static final Level TRACE = Level.FINEST;
	public static final Level DEBUG = Level.FINE;
	public static final Level INFO = Level.INFO;
	public static final Level WARN = Level.WARNING;
	public static final Level FATAL = Level.SEVERE;
	public static final Level ERROR = Level.SEVERE;

	private Logger logger = null;

	JdkLogger(String className) {
		logger = Logger.getLogger(className);

		isFatalEnabled = logger.isLoggable(FATAL);
		isErrorEnabled = logger.isLoggable(ERROR);
		isWarnEnabled = logger.isLoggable(WARN);
		isInfoEnabled = logger.isLoggable(INFO);
		isDebugEnabled = logger.isLoggable(DEBUG);
		isTraceEnabled = logger.isLoggable(TRACE);
	}

	public void debug(Object message, Throwable cause) {
		if (isDebugEnabled())
			logMessage(DEBUG, message, cause);
	}

	public void error(Object message, Throwable cause) {
		if (isErrorEnabled())
			logMessage(ERROR, message, cause);
	}

	public void fatal(Object message, Throwable cause) {
		if (isFatalEnabled())
			logMessage(FATAL, message, cause);
	}

	public void info(Object message, Throwable cause) {
		if (isInfoEnabled())
			logMessage(INFO, message, cause);
	}

	public void trace(Object message, Throwable cause) {
		if (isTraceEnabled())
			logMessage(TRACE, message, cause);
	}

	public void warn(Object message, Throwable cause) {
		if (isWarnEnabled())
			logMessage(WARN, message, cause);
	}

	private void logMessage(Level level, Object message, Throwable cause) {
		if (cause != null)
			logger.log(level, message.toString(), cause);
		else
			logger.log(level, message.toString());
	}
}
